<!DOCTYPE html>
<html lang="en" ng-app="TweetSearch">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <meta name="description" content="">
        <meta name="author" content="">
        <link rel="icon" type="image/png" href="../../artwork/favicon.png">
        <title>Sentimental Analysis</title>
        <!-- Bootstrap core CSS -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
        <link rel="stylesheet" href="css/ie10-viewport-bug-workaround.css">
        <!-- Custom styles for this template -->
        <link href="css/starter-template.css" rel="stylesheet">
        <script src="../../js/jquery.min.js"></script>
        <script src="../../js/angular.min.js"></script>
        <script src="js/highcharts.min.js"></script>
        <script src="js/exporting.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>

        <style>
            .post-wrapper:first-child .post-inner {
            border-top-color: rgba(0, 0, 0, 0.1);
            }
            .post-wrapper .post-inner {
            background-color: white;
            display: block;
            cursor: pointer;
            overflow: hidden;
            text-decoration: none;
            color: #666666;
            -webkit-transition-duration: 0.25s;
            transition-duration: 0.25s;
            -webkit-transition-timing-function: ease-out;
            transition-timing-function: ease-out;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
            padding: 20px;
            }
            .post-wrapper .post-inner.open {
            margin-top: 20px;
            margin-bottom: 20px;
            border-radius: 5px;
            box-shadow: 0 4px 1px rgba(0, 0, 0, 0.2);
            border-bottom-color: transparent;
            }
            .post-wrapper .post-inner.open:hover {
            background-color: white;
            }
            .post-wrapper .post-inner:hover {
            background-color: #f0f2f5;
            }
            .post-wrapper .post-inner h2 {
            font-weight: 400;
            color: #54626C;
            }
            .post-wrapper .post-inner .text {
            display: none;
            }
            .post-wrapper .post-inner p {
            font-weight: 300;
            padding: 10px 0;
            }
            .post-wrapper .post-inner p.lead {
            font-size: 1.2em;
            }
            .Emoji.Emoji--forText {
            width: 2%;
            height: 2%;
            padding: initial;
            margin-left: 7px;
            }
        </style>
    </head>
    <body ng-controller="Controller">
        <nav class="navbar navbar-default navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed"
                            data-toggle="collapse" data-target="#navbar"
                            aria-expanded="false" aria-controls="navbar">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="/sentimentVisualizer/index.html">Sentiment Analysis</a>
                </div>
                <div id="navbar" class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">Visualize</a></li>
                    </ul>
                    <ul class="nav navbar-nav navbar-right">
                        <form class="navbar-form hidden-xs ng-pristine ng-valid">
                            <div class="form-group">
                                <div class="input-group input-group-xs ">
                                    <input name="q" type="text" id="query"
                                           class="form-control" ng-model="query"
                                           title="Search Twitter and the loklak network"
                                           placeholder="Search for a tweet text"
                                           ng-init="query = ''"
                                           ng-keyup="$event.keyCode == 13 && query != '' ? Search() : null"
                                           oninvalid="this.setCustomValidity('Please Enter something to Visualize')"
                                           ng-required="true">
                                    <span class="input-group-btn">
                                    <button class="btn btn btn-primary" href='#'
                                            type="button" id="button"
                                            ng-click="query != '' ? Search() : null">Visualize Emotion</button>
                                    </span>
                                </div>
                            </div>
                        </form>
                    </ul>
                </div>
                <!--/.nav-collapse -->
            </div>
        </nav>
        <div class="container">
            <br></br>
            <h1>Emotion and Data Visualization for: {{query}}</h1>
        <center><div ng-if="spinner && !tweetsStatus"><img src="loading.gif" height="400" width="400"/></div></center>
        <div style="width: 100%;margin: 0 auto;">
            <div style="width: 50%;min-height: 400px;float: left;" class="pie-chart2"></div>
            <div style="width: 50%;min-height: 400px;float: left;" class="pie-chart"></div>
        </div>
            <div ng-if="tweetsStatus">
            <div class="row" ng-repeat="value in tweetsStatus | limitTo:10">
                <div class="post-wrapper">
                    <div class="post-inner open">
                        <h2>{{value[0]}}<code style="text-align:right">{{value[1]}}</code></h2>
                        <p class="lead">{{value[2]}}</p>
                    </div>
                </div>
            </div>
            </div>
        </div>
    </body>
    <script>
         var app = angular.module('TweetSearch', []);
         app.controller('Controller', ['$scope', '$http', '$sce', function($scope, $http, $sce) {

             $scope.Search = function() {
                $scope.spinner=true;
                 var QueryCommand = 'https://api.loklak.org/api/search.json?callback=JSON_CALLBACK&q=' + $scope.query;

                 $http.jsonp(String(QueryCommand)).success(function(response) {

                     var data = response;
                     var statuses = data.statuses;
                     var users = [];
                     var emotion = [];
                     var text = [];
                     var count = [];
                     var emotions_array = [];
                     var language_array = [];
                     for (var i=0; i<statuses.length; i++) {
                        if(statuses[i].classifier_emotion){
                            user = statuses[i].screen_name;
                            emotion = statuses[i].classifier_emotion;
                            emotions_array.push(emotion);
                            text = statuses[i].text;
                            users.push([user, emotion, text]);
                        }
                        if(statuses[i].classifier_language){
                            lang = statuses[i].classifier_language;
                            language_array.push(lang);
                        }
                     }
                     emotions_array.sort();
                        var current = null;
                        var em_count = 0;
                        var emotions_name = [];
                        var key;
                        var y;
                        for (var i = 0; i <= emotions_array.length; i++) {
                            var emotionsObj =  {};
                            if (emotions_array[i] != current) {
                                if (em_count > 0) {
                                    emotionsObj['name'] = current;
                                    emotionsObj['y'] = em_count;
                                    emotions_name.push(emotionsObj);
                                }
                                current = emotions_array[i];
                                em_count = 1;
                                } else {
                                em_count++;
                            }
                        }
                    language_array.sort();
                    var lang_name = [];
                    for (var i = 0; i <= language_array.length; i++) {
                            var languageObj =  {};
                            if (language_array[i] != current) {
                                if (em_count > 0) {
                                    languageObj['name'] = current;
                                    languageObj['y'] = em_count;
                                    lang_name.push(languageObj);
                                }
                                current = language_array[i];
                                em_count = 1;
                                } else {
                                em_count++;
                            }
                        }
                     $scope.tweetsStatus = users;
                     $scope.emotions = emotions_name;
                     $scope.languages = lang_name;
                     $('.pie-chart').highcharts({
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false,
                            type: 'pie'
                        },
                        title: {
                            text: $scope.query
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    format : '',
                                    style: {
                                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                    }
                                }
                            }
                        },
                        series: [{
                            name: $scope.query,
                            colorByPoint: true,
                            data: $scope.emotions
                        }]
                    });
                     $('.pie-chart2').highcharts({
                        chart: {
                            plotBackgroundColor: null,
                            plotBorderWidth: null,
                            plotShadow: false,
                            type: 'pie'
                        },
                        title: {
                            text: $scope.query
                        },
                        tooltip: {
                            pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                        },
                        plotOptions: {
                            pie: {
                                allowPointSelect: true,
                                cursor: 'pointer',
                                dataLabels: {
                                    enabled: true,
                                    format : '',
                                    style: {
                                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                                    }
                                }
                            }
                        },
                        series: [{
                            name: $scope.query,
                            colorByPoint: true,
                            data: $scope.languages
                        }]
                    });
                 });
             }
             $scope.spinner = false;
         }]);

      </script>
</html>
